package judesart.app.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import judesart.app.domain.Artist;
import judesart.app.domain.Artwork;
import judesart.app.domain.ArtworkPhoto;
import judesart.app.domain.User;

import org.springframework.web.servlet.view.feed.AbstractRssFeedView;

import ac.simons.syndication.modules.atom.AtomContent;
import ac.simons.syndication.modules.atom.AtomModuleImpl;
import ac.simons.syndication.utils.SyndicationLink;

import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Description;
import com.sun.syndication.feed.rss.Enclosure;
import com.sun.syndication.feed.rss.Guid;
import com.sun.syndication.feed.rss.Image;
import com.sun.syndication.feed.rss.Item;

public class RssArtworksFeedView extends AbstractRssFeedView {

    private final static Date DEFAULT_CREATEDAT;
    static {
        try {
            DEFAULT_CREATEDAT = new SimpleDateFormat("dd/MM/yyyy").parse("1/12/2010");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void buildFeedMetadata(Map<String, Object> model, Channel feed,
            HttpServletRequest request) {
        feed.setTitle("Judith Shaylor Artworks");
        feed.setDescription("Judith Shaylor Artworks");
        feed.setLink("http://www.judithshaylor.com/");
        Image image = new Image();
        image.setDescription("Judith Shaylor Art");
        Long imageId = ((Artist) model.get("artist")).getPhotos().get(0).getImageId();
        image.setUrl("http://www.judithshaylor.com/image?id=" + imageId
                + "&size=100x100&crop=true");
        image.setTitle("Judith Shaylor Artworks");
        image.setLink("http://www.judithshaylor.com/");
        feed.setImage(image);
        final AtomContent atomContent = new AtomContent();
        atomContent.addLink(new SyndicationLink().withRel("self").withHref(
                "http://www.judithshaylor.com/web/feeds/artworks.rss").getLink());
        atomContent.addLink(new SyndicationLink().withRel("alternate").withType(
                "text/html").withHref("www.judithshaylor.com").getLink());
        feed.getModules().add(new AtomModuleImpl(atomContent));
    }

    @Override
    protected List<Item> buildFeedItems(Map<String, Object> model,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        @SuppressWarnings("unchecked")
        List<Artwork> artworks = (List<Artwork>) model.get("artworkList");
        User user = (User) model.get("user");

        List<Item> feedItems = new ArrayList<Item>();
        for (Artwork artwork : artworks) {
            Item feedItem = new Item();
            Guid guid = new Guid();
            guid.setValue("http://www.judithshaylor.com/#gallery/artwork/"
                    + artwork.getId());
            feedItem.setGuid(guid);
            feedItem.setTitle(artwork.getTitle());
            feedItem.setAuthor(user.getEmail() + " (" + user.getName() + ")");
            if (artwork.getCreatedAt() != null)
                feedItem.setPubDate(artwork.getCreatedAt());
            else
                feedItem.setPubDate(DEFAULT_CREATEDAT);

            Description desc = new Description();
            desc.setType("text/html");
            desc.setValue(artwork.getDescription());
            feedItem.setDescription(desc);

            feedItem.setLink("http://www.judithshaylor.com/#gallery/artwork/"
                    + artwork.getId());
            List<ArtworkPhoto> phts = artwork.getPhotos();
            if (phts != null && phts.size() > 0) {
                List<Enclosure> enclosures = new ArrayList<Enclosure>();
                Enclosure enclosure = new Enclosure();
                Long imageId = phts.get(0).getImageId();
                enclosure.setUrl("http://www.judithshaylor.com/image?id="
                        + imageId + "&size=100x100&crop=true");
                enclosure.setLength(100);
                enclosure.setType("image/jpg");
                enclosures.add(enclosure);
                feedItem.setEnclosures(enclosures);
            }
            feedItems.add(feedItem);
        }

        return feedItems;
    }
}
